package com.msmall.community.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.msmall.base.util.StringUtil;

public class SelectIncrementTag extends TagSupport {

	private static final long serialVersionUID = -131181397178917083L;

	private String id;
	
	private int start; // SALARY  CHARACTEREISE

	private int end;
	
	private int step;
	
	private String name;
	
	private String css;
	
	private int selected;
	
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}
	
	public int doEndTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		StringBuffer buf = new StringBuffer();
		try{
				buf.append("<select name=\"").append(name).append("\"");
				if(!StringUtil.nullOrBlank(id)){
					buf.append(" id=\"").append(id).append("\"");
				}
				if(!StringUtil.nullOrBlank(css)){
					buf.append(" class=\"").append(css).append("\"");
				}
				buf.append(">");
				for(int i=start; i<=end;){
					String value = String.valueOf(i);
					if(i == selected){
						buf.append(getOption(value, value, true ));
					}else{
						buf.append(getOption(value, value, false ));
					}
					i+=step;
				}
				buf.append("</select>");
			writer.print(buf.toString());
		}catch(Exception e){
			
		}
		return (SKIP_BODY);
	}
	
	private String getOption(String value, String message, boolean selected){
		StringBuffer buf = new StringBuffer();
		buf.append("<option value=\"").append(value).append("\"");
		if(selected){
			buf.append("selected=\"selected\"");
		}
		buf.append(">").append(message).append("</option>");
		return buf.toString();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCss() {
		return css;
	}

	public void setCss(String css) {
		this.css = css;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public void setStep(int step) {
		this.step = step;
	}

	public void setSelected(int selected) {
		this.selected = selected;
	}
}
